﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using System.Xml.Serialization;

namespace ZhangZhao.WebAPI
{
    public partial class UserRelationship : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string UserName = HttpContext.Current.Request[Parameter.UserName];
            string RelationShip = HttpContext.Current.Request[Parameter.RelationShip];
            int Number = int.MaxValue;
            int.TryParse(HttpContext.Current.Request[Parameter.Number], out Number);
            int StartIndex = 0;
            int.TryParse(HttpContext.Current.Request[Parameter.StartIndex], out StartIndex);

            if (StartIndex == 0)
                StartIndex = 1;

            string Output = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Output]);

            string result = string.Empty;

            #region
            if (!string.IsNullOrWhiteSpace(UserName))
            {
                ZhangZhao.BLL.H_User bll = new BLL.H_User();
                ZhangZhao.BLL.H_UserAvatar service = new BLL.H_UserAvatar();
                string where = string.Empty;
                Dictionary<string, string> dic = new Dictionary<string, string>();

                //RelationShip=1, 获取粉丝; RelationShip=2, 获取黑名单
                where = string.Format(" UserName in (select OtherUserName from dbo.H_UserRelationship with (nolock) where UserName = '{0}' and RelationShip = '{1}' ) ", UserName, RelationShip);

                List<ZhangZhao.Model.H_User> list = bll.GetModelListByPage(where, "Id", (StartIndex - 1) * Number, StartIndex * Number);
                if (list != null && list.Count > 0)
                {
                    foreach (ZhangZhao.Model.H_User u in list)
                    {
                        ZhangZhao.Model.H_UserAvatar avatar = service.GetModel(u.UserName);

                        if (!dic.ContainsKey(u.UserName))
                        {
                            dic.Add(u.UserName, avatar == null ? string.Empty : avatar.Avatar);
                        }
                    }
                }

                if (dic.Count > 0)
                {
                    if (!string.IsNullOrWhiteSpace(Output) && Output.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                    {
                        result = JsonConvert.SerializeObject(dic);
                    }
                    else
                    {
                        using (MemoryStream Stream = new MemoryStream())
                        {
                            XmlSerializer xml = new XmlSerializer(typeof(ZhangZhao.Model.UserCurrentInfo));

                            try
                            {
                                xml.Serialize(Stream, dic);
                            }
                            catch { }

                            Stream.Position = 0;
                            using (StreamReader sr = new StreamReader(Stream))
                            {
                                result = sr.ReadToEnd();
                            }

                            xml = null;
                        }
                    }
                }
            }
            #endregion


            Response.Clear();
            Response.Write(result);
            Response.End();
        }
    }
}